package com.ynkj.teleprompter.activity;

import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ynkj.teleprompter.BaseActivity;
import com.ynkj.teleprompter.R;
import com.ynkj.teleprompter.db.Sentence;
import com.ynkj.teleprompter.utiles.GlobalDialogSingle;
import com.ynkj.teleprompter.utiles.SharedPreferencesUtil;
import com.ynkj.teleprompter.utiles.Utilities;
import com.ynkj.teleprompter.view.ColorTrackView;
import com.ynkj.teleprompter.view.FloatWinfowServices;
import com.ynkj.teleprompter.view.VerticalScrollTextView;
import com.ynkj.teleprompter.view.printTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TcActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.scoll_textView)
    TextView scollTextView;
    @BindView(R.id.btn_start)
    Button btnStart;
    @BindView(R.id.btn_jd)
    Button btnJd;
    @BindView(R.id.btn_sx)
    Button btnSx;
    @BindView(R.id.btn_jk)
    Button btnJk;
    @BindView(R.id.btn_man)
    Button btnMan;
    private long sudu=100;
    private float daxiao=15;
    private boolean hasBind = false;

    @Override
    public int setLayout() {
        return R.layout.activity_tc;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        scollTextView.setText(MainActivity.content);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        stopThread();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("RemoteView", "重新显示了");
//        //不显示悬浮框
        if (hasBind) {
            unbindService(mVideoServiceConnection);
            hasBind = false;
        }
    }
//
//    public void stopThread(){
//        if(myThread!=null){
//            Log.e("msss","0000000000000");
//            myThread.interrupt();
//            myThread = null;
//        }
//    }



    @OnClick({R.id.btn_jk, R.id.btn_man,R.id.btn_jd, R.id.btn_sx, R.id.btn_start})

    public void onBindClick(View view) {
        switch (view.getId()) {
            case R.id.btn_jk:
                if(sudu>20){
                    sudu=sudu-20;
                    SharedPreferencesUtil.putLong(mContext,"sudu",sudu);
                }else {
                    Utilities.TOAST(mContext,"已经不能在快了");
                }
                break;
            case R.id.btn_man:
                if(sudu<=200){
                    sudu=sudu+20;
                    SharedPreferencesUtil.putLong(mContext,"sudu",sudu);
                }else {
                    Utilities.TOAST(mContext,"已经不能在慢了");
                }
                break;
            case R.id.btn_jd:
                if(daxiao<=50){
                    daxiao=daxiao+1;
                    scollTextView.setTextSize(daxiao);
                    SharedPreferencesUtil.putFloat(mContext,"daxiao",daxiao);
                }else {
                    Utilities.TOAST(mContext,"已经不能在大了");
                }
                break;
            case R.id.btn_sx:
                if(daxiao>=15){
                    daxiao=daxiao-1;
                    scollTextView.setTextSize(daxiao);
                    SharedPreferencesUtil.putFloat(mContext,"daxiao",daxiao);
                }else {
                    Utilities.TOAST(mContext,"已经不能在小了");
                }
                break;
            case R.id.btn_start:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!Settings.canDrawOverlays(mContext)) {
                        Toast.makeText(mContext, "当前无权限，请授权", Toast.LENGTH_SHORT);
                        new GlobalDialogSingle(mContext, "", "当前未获取悬浮窗权限", "去开启", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + mContext.getPackageName())), 0);
                            }
                        }).show();
                    } else {
                        moveTaskToBack(true);
                        Intent intent = new Intent(mContext, FloatWinfowServices.class);
                        hasBind = bindService(intent, mVideoServiceConnection, Context.BIND_AUTO_CREATE);
                    }
                }
                break;
        }
    }


    ServiceConnection mVideoServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 获取服务的操作对象
            FloatWinfowServices.MyBinder binder = (FloatWinfowServices.MyBinder) service;
            binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(mContext)) {
                    Toast.makeText(mContext, "授权失败", Toast.LENGTH_SHORT).show();
                } else {

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent(mContext, FloatWinfowServices.class);
                            hasBind = bindService(intent, mVideoServiceConnection, Context.BIND_AUTO_CREATE);
                            moveTaskToBack(true);
                        }
                    },1000);

                }
            }
        }
    }
}
